package soatonline.VO; 

import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;

public class DatePicker {  

int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
JLabel l = new JLabel("", JLabel.CENTER);  
String day = "";
JDialog d;
JLabel[] button = new JLabel[49];
JPanel p1;
JButton previous,previouss,next,nextt;
java.util.Calendar cal;

public DatePicker(final JTextField parent) {  


d = new JDialog();
d.setResizable(false);
d.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
d.setModal(true);
String[] header = { "Lun", "Mar", "Mie", "Jue", "Vie", "Sab", "Dom" };
p1 = new JPanel(new GridLayout(7, 7));
p1.setPreferredSize(new Dimension(290, 160));
p1.setBackground(Color.WHITE);
for (int x = 0; x < button.length; x++) {
    
        
    final int selection = x;
    button[x] = new JLabel();
    button[x].setBackground(Color.white);
    
    if (x > 6)
        button[x].addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        MouseClicked(evt);
    }

        private void MouseClicked(MouseEvent evt) {
            day = button[selection].getText();
            d.dispose();
        }
    });
        if (x < 7) {
            button[x].setText(header[x]);
            button[x].setForeground(Color.red);
            
        }
        p1.add(button[x]);
}

JPanel p2 = new JPanel(new GridLayout(1, 4));
previouss = new JButton("<<");

previouss.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        year--;
        displayDate();
    }
});


p2.add(previouss);
previous = new JButton("<");

previous.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        
        month--;
        displayDate();
    }
});

p2.add(previous);
//p2.add(l);
next = new JButton(">");

next.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        
        month++;
        
        displayDate();
    }
});

p2.add(next);
nextt = new JButton(">>");
nextt.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        year++;
        displayDate();
    }
});

p2.add(nextt);
p2.setPreferredSize(new Dimension(280,40));
JPanel p3 = new JPanel(new GridLayout(1, 1));
p3.add(l);
d.add(p1, BorderLayout.NORTH);
d.add(p2, BorderLayout.CENTER);
d.add(p3,BorderLayout.SOUTH);

d.pack();
d.setLocationRelativeTo(parent);
displayDate();
d.setVisible(true);

}
public void desactivarBoton(int boton){
    switch(boton){
    
        case 1:
            this.previouss.setEnabled(false);
            break;
        case 2:
            this.previous.setEnabled(false);
            break;
        case 3:
            this.next.setEnabled(false);
            break;
        case 4:
            this.nextt.setEnabled(false);
            break;
        
    }

}

public void activarBoton(int boton){
    switch(boton){
    
        case 1:
            this.previouss.setEnabled(true);
            break;
        case 2:
            this.previous.setEnabled(true);
            break;
        case 3:
            this.next.setEnabled(true);
            break;
        case 4:
            this.nextt.setEnabled(true);
            break;
        
    }

}

public void displayDate() {
    for (int x = 7; x < button.length; x++)
        button[x].setText("");
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMMM yyyy");
    cal = java.util.Calendar.getInstance();
    cal.set(year, month, 1);
    int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);
    int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
    for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++)
        button[x].setText("" + day);
    l.setText(sdf.format(cal.getTime()));
    l.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N


    d.setTitle("Fecha");
    this.p1.repaint();

}

public String setPickedDate() {
    if (day.equals(""))
        return day;
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
    java.util.Calendar cal = java.util.Calendar.getInstance();
    cal.set(year, month, Integer.parseInt(day));
    return sdf.format(cal.getTime());
}

    JTextField setPickedDate(Object object) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

 
